/****************************************************
	文件：PostEffectsBase.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/8/12 16:52:34
	功能：屏幕后处理（基类）
*****************************************************/

using System;
using UnityEngine;

namespace RenderingPractice
{
    [ExecuteInEditMode, RequireComponent(typeof(Camera))]
    public class PostEffectsBase : MonoBehaviour
    {
        private void Start()
        {
            CheckResource();
        }

        /// <summary>
        /// 检测
        /// </summary>
        protected void CheckResource()
        {
            bool isSupported = CheckSupport();
            if (isSupported == false)
                NoSupported();
            else
                Supported();
        }

        /// <summary>
        /// 检测Shader是否可用，然后返回该Shader的材质
        /// </summary>
        /// <param name="shader"></param>
        /// <param name="material"></param>
        /// <returns></returns>
        protected Material CheckShaderAndCreateMaterial(Shader shader, Material material)
        {
            if (shader == null || shader.isSupported == false) return null;
            if (shader.isSupported && material && material.shader == shader)
            {
                return material;
            }
            material = new Material(shader);
            material.hideFlags = HideFlags.DontSave;
            return material;
        }
        /// <summary>
        /// 检测硬件是否支持屏幕后处理
        /// </summary>
        /// <returns></returns>
        protected bool CheckSupport()
        {
            return true;
        }
        /// <summary>
        /// 不支持情况下
        /// </summary>
        protected void NoSupported()
        {
            enabled = false;
        }
        /// <summary>
        /// 支持的情况下
        /// </summary>
        protected void Supported()
        {
            enabled = true;
        }
    }
}
